﻿// Copyright (c) Omar AL Zabir. All rights reserved.
// For continued development and updates, visit http://msmvps.com/omar

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.Security;
using System.Transactions;
using System.Linq;
using System.Data.Linq;

using DataLayer;

namespace BusinessLayer
{
   public class Facade
   {
      private Guid _UserGuid;
      private string _UserName;

      public Facade(Guid userGuid, string userName)
      {
         _UserGuid = userGuid;
         _UserName = userName;
      }

      public void Tab_UpdateTitle(int TabID, string Title)
      {
         using (new TimedLog(this._UserName, "Tab_UpdateTitle", TabID + ":" + Title))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("Title", Title);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_UpdateTitle), properties);
         }
      }

      public void Tab_Delete(int TabID, int NewActiveTabId)
      {
         using (new TimedLog(this._UserName, "Tab_Delete", TabID.ToString()))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("NewActiveTabId", NewActiveTabId);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_Delete), properties);
         }
      }

      public TabWidget Tab_AddWidget(int TabID, int WidgetId, string State)
      {
         return Tab_AddWidget(TabID, WidgetId, State, null, null);
      }
      public TabWidget Tab_AddWidget(int TabID, int WidgetId, string State, string Title, string Icon)
      {
         using (new TimedLog(this._UserName, "Tab_AddWidget", TabID + ":" + WidgetId + ":" + Title))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("WidgetId", WidgetId);
            properties.Add("WidgetInstance", null);
            properties.Add("State", State);
            properties.Add("Title", Title);
            properties.Add("Icon", Icon);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_AddWidget), properties);
            return properties["WidgetInstance"] as TabWidget;
         }
      }

      public void Tab_DeleteWidget(int TabID, int WidgetId)
      {
         using (new TimedLog(this._UserName, "Tab_DeleteWidget", TabID + ":" + WidgetId))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("WidgetId", WidgetId);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_DeleteWidget), properties);
         }
      }

      public int Tab_Create()
      {
         using (new TimedLog(this._UserName, "Tab_Create"))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", 0);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_Create), properties);
            return (int)properties["TabId"];
         }
      }

      public void Tab_ResizeColumns(int TabID, byte C1, byte C2, byte C3, byte C4, string innerHtml)
      {
         using (new TimedLog(this._UserName, "Tab_ResizeColumns", TabID + ":" + C1 + ":" + C2 + ":" + C3 + ":" + C4 + ":" + innerHtml))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("C1", C1);
            properties.Add("C2", C2);
            properties.Add("C3", C3);
            properties.Add("C4", C4);
            properties.Add("innerHTML", innerHtml);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_ResizeColumns), properties);
         }
      }

      public void Tab_ReorderWidget(int TabID, int ColumnNo, string OrderStr)
      {
         using (new TimedLog(this._UserName, "Tab_ResizeColumns", TabID + ":" + ColumnNo + ":" + OrderStr))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("ColumnNo", ColumnNo);
            properties.Add("OrderStr", OrderStr);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_ReorderWidget), properties);
         }
      }
      
      public void WidgetInstance_UpdateProperty(int TabID, int WidgetInstanceId, string Property, string Value)
      {
         using (new TimedLog(this._UserName, "WidgetInstance_UpdateProperty", TabID + ":" + WidgetInstanceId+ ":" + Property + ":" + Value))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("WidgetInstanceId", WidgetInstanceId);
            properties.Add("wProperty", Property);
            properties.Add("wValue", Value);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.WidgetInstance_UpdateProperty), properties);
         }
      }

      public void WidgetInstance_UpdateProperties(int TabID, int WidgetInstanceId, Dictionary<string, string> wProperties)
      {
         using (new TimedLog(this._UserName, "WidgetInstance_UpdateProperties", TabID + ":" + WidgetInstanceId))
         {
            var properties = new Dictionary<string, object>();
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("WidgetInstanceId", WidgetInstanceId);
            properties.Add("wProperties", wProperties);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.WidgetInstance_UpdateProperties), properties);
         }
      }

      public bool Tab_CheckReadPermission(int TabID)
      {
         using (new TimedLog(this._UserName, "Tab_Create"))
         {
            var properties = new Dictionary<string, object>();
            bool result = false;
            properties.Add("UserGuid", this._UserGuid);
            properties.Add("TabId", TabID);
            properties.Add("Result", result);

            WorkflowHelper.ExecuteWorkflow(typeof(Workflows.Tab_CheckReadPermission), properties);
            return (bool)properties["Result"];
         }
      }
   }
}

